package cn.lblbc.lib.view

import android.content.Context
import android.net.Uri
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
import cn.lblbc.lib.R
import com.bumptech.glide.Glide

class ImageViews(context: Context, attrs: AttributeSet?) : ScrollView(context, attrs) {

    var imagesLayout: LinearLayout

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.lib_view_images, this, true)
        imagesLayout = view.findViewById(R.id.imagesLayout)
    }

    fun setData(imageUrls: String) {
        imageUrls.split(";").filter { it.isNotEmpty() }.forEach {
            addImageView(it)
        }
    }

    private fun addImageView(imageUrl: String) {
        val imageView = ImageView(context)
        imageView.setImageURI(Uri.parse(imageUrl))
        Glide.with(context).load(imageUrl).into(imageView)
        imagesLayout.addView(imageView)
    }
}